
package com.rx.mgt.service;  

import java.util.List;
import java.util.Map;

import com.rx.mgt.util.StringUtil;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.rx.mgt.dao.UserQuotaGroupDao;
import com.rx.mgt.domain.UserQuotaGroup;
import com.rx.mgt.po.UserGroupCountBo;
import com.rx.mgt.po.UsersBo;

/** 
 * ClassName:UuserQuotaGroupService 
 * Function :
 * Reason   : 
 * Date     :Jan 31, 2017 9:21:51 PM 
 * @author   luolei 
 * @since    JDK 1.6
 */
@Service
public class UserQuotaGroupService {
    @Autowired
    UserQuotaGroupDao userQuotaGroupDao;
    @Autowired
    UserInfoExtService userInfoExtService;
    public UserQuotaGroup get(Long id){
        return  userQuotaGroupDao.get(id);
    }
    public List<UserQuotaGroup> getAllQuotaGroup(UsersBo usersBo){
        List<UserQuotaGroup> list =  userQuotaGroupDao.getAllQuotaGroup(usersBo);
        List<UserGroupCountBo>  boList =  userInfoExtService.getAllUserGroupCount();
        for(UserQuotaGroup uqg:list){
            long uqgId=uqg.getId();
            for(UserGroupCountBo bo:boList){
                long boId = bo.getId().longValue();
                if(uqgId==boId){
                    uqg.setCounts(bo.getCounts().longValue());
                }
            }
        }
        return list;
    }
    public Long saveOrUpdate(UserQuotaGroup userQuotaGroup){
        if(userQuotaGroup.getId()!=null&&userQuotaGroup.getId()>0){
            userQuotaGroupDao.update(userQuotaGroup);
            return userQuotaGroup.getId();
        }else{
            return (Long) userQuotaGroupDao.save(userQuotaGroup);
        }
    }
    public void delete(Long id){
        userQuotaGroupDao.delete(userQuotaGroupDao.get(id));
    }

    public List<Map<String,Object>> getGroupByQuotaGroupName(String name) {
        return userQuotaGroupDao.getGroupByQuotaGroupName(name);
    }

    public List<Map<String,Object>> getGroupMembers(String name,String departmentName) {
        if(StringUtils.isNotEmpty(name)&&StringUtils.isEmpty(departmentName)){
            return userQuotaGroupDao.getGroupMembersByGroupName(name);
        }else{
            return userQuotaGroupDao.getGroupMembersByDepartMentName(name,departmentName);
        }

    }

    public int addOrUpdateGroupName(String id, String pId, String name) {
        int i = 0 ;
        if(StringUtils.isEmpty(pId)){
            Map<String,Object> p =  userQuotaGroupDao.findGroupByName(name);
            if(p==null){
               if(StringUtils.isEmpty(id)){
                   i = userQuotaGroupDao.addGroup(name);
               }else{
                   i = userQuotaGroupDao.updateGroup(id,name);
               }
            }

        }else{
            Map<String,Object> p =  userQuotaGroupDao.findDepartMentByName(pId,name);
            if(p==null&&StringUtils.isEmpty(id)){
                i = userQuotaGroupDao.addDepartMent(name,pId);
            }else{
                i = userQuotaGroupDao.updateDepartMent(id,name);
            }
        }
        return i;
    }

    public void addGroup(Map<String, Object> rs) {
       long id =  userQuotaGroupDao.queryMaxGroupId();
        if(userQuotaGroupDao.addGroup("新建企业1")>0){
            id++;
            rs.put("id",id);

        }

    }
}
  